using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Dal.Helper;
using EP.Base.BaseService.Manager.Interface.Data;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Dal
{    
	/// <summary>
    /// EsTaskConfig 
    /// </summary>
    public class EsTaskConfigs:IEsTaskConfigs
    {

		/// <summary>
		/// 添加EsTaskConfig
		/// </summary>
		/// <param name="estaskconfig"></param>
		/// <returns></returns>
		public int Add(EsTaskConfig estaskconfig)
		{
			string sql = @"INSERT INTO [EsTaskConfig]
			([IndexConfigId],[Type],[Configs],[Cron],[LastRunTime],[LastRunResult],[LastRunLog],[State],[Interval],[CreateTime])
			VALUES
			(@IndexConfigId,@Type,@Configs,@Cron,@LastRunTime,@LastRunResult,@LastRunLog,@State,@Interval,@CreateTime) 
			SELECT SCOPE_IDENTITY()
			";
			var par = new DynamicParameters();
			par.Add("@IndexConfigId",estaskconfig.IndexConfigId , DbType.Int32);
			par.Add("@Type",estaskconfig.Type , DbType.Int16);
			par.Add("@Configs",estaskconfig.Configs , DbType.AnsiString);
			par.Add("@Cron",estaskconfig.Cron , DbType.AnsiString);
			par.Add("@LastRunTime",estaskconfig.LastRunTime , DbType.DateTime);
			par.Add("@LastRunResult",estaskconfig.LastRunResult , DbType.Int16);
			par.Add("@LastRunLog",estaskconfig.LastRunLog , DbType.String);
			par.Add("@State",estaskconfig.State , DbType.Int16);
			par.Add("@Interval",estaskconfig.Interval , DbType.Int32);
			par.Add("@CreateTime",estaskconfig.CreateTime , DbType.DateTime);
			return DapWrapper.InnerQueryScalarSql<int>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 修改EsTaskConfig
		/// </summary>
		/// <param name="estaskconfig"></param>
		/// <returns></returns>
		public int Update(EsTaskConfig estaskconfig)
		{
			string sql = @"UPDATE  [EsTaskConfig] SET 
						IndexConfigId=@IndexConfigId,
						Type=@Type,
						Configs=@Configs,
						Cron=@Cron,
						LastRunTime=@LastRunTime,
						LastRunResult=@LastRunResult,
						LastRunLog=@LastRunLog,
						State=@State,
						Interval=@Interval,
						CreateTime=@CreateTime
 WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", estaskconfig.Id, DbType.Int32);
			par.Add("@IndexConfigId",estaskconfig.IndexConfigId , DbType.Int32);
			par.Add("@Type",estaskconfig.Type , DbType.Int16);
			par.Add("@Configs",estaskconfig.Configs , DbType.AnsiString);
			par.Add("@Cron",estaskconfig.Cron , DbType.AnsiString);
			par.Add("@LastRunTime",estaskconfig.LastRunTime , DbType.DateTime);
			par.Add("@LastRunResult",estaskconfig.LastRunResult , DbType.Int16);
			par.Add("@LastRunLog",estaskconfig.LastRunLog , DbType.String);
			par.Add("@State",estaskconfig.State , DbType.Int16);
			par.Add("@Interval",estaskconfig.Interval , DbType.Int32);
			par.Add("@CreateTime",estaskconfig.CreateTime , DbType.DateTime);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}

	    public bool UpdateLastRuanInfo(EsTaskConfig estaskconfig)
	    {
            string sql = @"UPDATE  [EsTaskConfig] SET 
						LastRunTime=@LastRunTime,
						LastRunResult=@LastRunResult,
						LastRunLog=@LastRunLog
                        WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@Id", estaskconfig.Id, DbType.Int32);
         
            par.Add("@LastRunTime", estaskconfig.LastRunTime, DbType.DateTime);
            par.Add("@LastRunResult", estaskconfig.LastRunResult, DbType.Int16);
            par.Add("@LastRunLog", estaskconfig.LastRunLog, DbType.String);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par)>0;
	    }

	    /// <summary>
		/// 根据id获取EsTaskConfig
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public EsTaskConfig Get(int id)
		{
			string sql = "select [Id],[IndexConfigId],[Type],[Configs],[Cron],[LastRunTime],[LastRunResult],[LastRunLog],[State],[Interval],[CreateTime] FROM [EsTaskConfig] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int32);
			return DapWrapper.InnerQuerySql<EsTaskConfig>(DbConfig.EpBaseServiceManagerConnectionString, sql, par).FirstOrDefault();
		}
		/// <summary>
		/// 根据id删除EsTaskConfig
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public Int32 Delete(int id)
		{
			string sql="DELETE [EsTaskConfig] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int32);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 根据ids删除EsTaskConfig多条记录
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		public Int32 Deletes(int[] ids)
		{
			if (ids.Length == 0)
                return 0;
			string sql="DELETE [EsTaskConfig] WHERE Id IN ("+string.Join(",",ids)+")";
			return DapWrapper.InnerExecuteText(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
		/// <summary>
       /// 获取EsTaskConfig分页列表
       /// </summary>
       /// <param name="pageIndex">页码</param>
       /// <param name="pageSize">每页显示条数</param>
       /// <param name="beginTime">开始时间</param>
       /// <param name="endTime">结束时间</param>
       /// <param name="recordCount">总记录数</param>
       /// <returns>EsTaskConfig列表</returns>
       public List<EsTaskConfig> GetPageList(int pageIndex, int pageSize, DateTime? beginTime, DateTime? endTime, out int recordCount)
       {
           recordCount = 0;
           var par = new DynamicParameters();
           par.Add("@PageIndex", pageIndex, DbType.Int32);
           par.Add("@PageSize", pageSize, DbType.Int32);
           par.Add("@BeginTime", beginTime, DbType.DateTime);
           par.Add("@EndTime", !endTime.HasValue ? endTime : endTime.Value.AddDays(1).AddMilliseconds(-1), DbType.DateTime);
           par.Add("@TotalCount", recordCount, DbType.Int32, ParameterDirection.Output);
           var result = DapWrapper.InnerQueryProc<EsTaskConfig>(DbConfig.EpBaseServiceManagerConnectionString, "proc_GetEsTaskConfigPageList", par);
           recordCount = par.Get<int>("@TotalCount");
           return result;
       }
	   /// <summary>
		/// 获取所有EsTaskConfig
		/// </summary>
		/// <returns></returns>
		public List<EsTaskConfig> GetAll()
		{
			string sql = "select [Id],[IndexConfigId],[Type],[Configs],[Cron],[LastRunTime],[LastRunResult],[LastRunLog],[State],[Interval],[CreateTime] FROM [EsTaskConfig]";
			
			return DapWrapper.InnerQuerySql<EsTaskConfig>(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
    }
}
